<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
if (!in_array($chkGametype, array(4)))
{
    exit();
}
$chk = new CheckMode_3d($curruser['acl'], 5, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$betname_3d = judge_type($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
$u_subsidary = get_user($curruser['id'], $gametype);
if ($curruser['acl'] != "5")
{
    exit();
}
$snow_open = $conf->get("snow_open_{$tbl}");
$stopbet = $conf->get("stopbet_{$tbl}");
if ($snow_open == "0" || $u_subsidary['snow_open'] == "0")
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"error:the function is closed.\"}";
    exit();
}
require( "class_sb/SRate_sb.php" );
require( "class_sb/RateConfig_sb.php" );
require( "class_sb/AutoRateConfig_sb.php" );
$conf = new Config();
$RateConfig_3d = new RateConfig_sb($gametype);
$AutoRateConfig_3d = new AutoRateConfig_sb($betname_3d, $g_autorate_3d, $gametype);
$SRate_3d = new SRate_sb($gametype);
$disk = 1;
$playtype = getvar("playtype");
$order = "0";
$split = "1";
$ratemod = "0";
$lotto_id = intval(getvar("lotto_id_3d"));
$bettype = getvar("actionBettype");
$numberStr = getvar("actionNum");
$mp = getvar("actionMP");
$addVal = floatval(getvar("addVal"));
$method = intval(getvar("method"));
if ($addVal < 0)
{
    $addVal = $addVal * ( 0 - 1 );
}
$errorMsg = "";
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    exit();
}
else if ($playtype == "" || !isset($RateConfig_3d->playGroupArr02[$playtype]))
{
    exit();
}
$bettypeArr = explode(",", $bettype);
$numArr = explode(",", $numberStr);
$autoRateArr = $AutoRateConfig_3d->getAutoRateAll();
$disChange = array();
$okFlag = true;
$sql = "SELECT round, open FROM lotto_{$tbl} WHERE id = {$lotto_id} LIMIT 1";
$rs = getdb($sql);
if ($rs->eof)
{
    exit();
}
$fieldArr = array("method" => $method, "gametype" => $gametype, "round" => $rs->f['round'], "lotto_id" => $lotto_id, "betcontent" => "", "orate" => "", "nrate" => "", "pagename" => "0", "paperopen" => $rs->f['open']);
$rs->close();
foreach ($bettypeArr as $valStr)
{
    $valArr = explode("_", $valStr);
    $val = $valArr[0];
    if (isset($valArr[1]))
    {
        $subid = $valArr[1];
    }
    else
    {
        $subid = 0;
    }
    $valIdx = "{$val}_{$subid}";
    if (!in_array($val, $RateConfig_3d->playGroupArr02[$playtype]))
    {
        exit();
    }
    if (!isset($autoRateArr[$val]) || $autoRateArr[$val]['money'] <= 0)
    {
        array_push($disChange, $valIdx);
        continue;
    }
    $betTypeNumberArr = $RateConfig_3d->getBetTypeNumber($val);
    if ($autoRateArr[$val]['typeflag'] == 1)
    {
        $addVal = $autoRateArr[$val]['downrate'];
    }
    $numRateArr = array_intersect($betTypeNumberArr, $numArr);
    if ($mp == "p")
    {
        $rateArr = $RateConfig_3d->setRateArrDir($numRateArr, 0);
    }
    else
    {
        $rateArr = $RateConfig_3d->setRateArrDir($numRateArr, $addVal);
    }
    if (0 < count($rateArr))
    {
        $rateOldArr = array();
        $rateNewArr = array();
        $rateStrOLD = $SRate_3d->getRatesS($lotto_id, $val, $curruser['id'], $subid);
        $rateNewArr[0] = $SRate_3d->getRates($lotto_id, 0, 1, $val, $subid);
        if ($mp == "p")
        {
            $okFlag = $SRate_3d->updateRateS($curruser['id'], $lotto_id, $val, $subid, $rateArr);
        }
        else
        {
            $okFlag = $SRate_3d->addUpdRatesS($curruser['id'], $lotto_id, $val, $subid, $rateArr);
        }
        $rateStrNEW = $SRate_3d->getRatesS($lotto_id, $val, $curruser['id'], $subid);
        foreach ($numRateArr as $newKey => $newVal)
        {
            foreach ($rateNewArr as $rateNewArrKey => $rateNewArrVal)
            {
                $rateOld = $rateNewArr[$rateNewArrKey][$newKey] - $rateStrOLD[$newKey];
                $rateNew = $rateNewArr[$rateNewArrKey][$newKey] - $rateStrNEW[$newKey];
                if ($rateOld != $rateNew)
                {
                    $fieldArr['betcontent'] = "[{$betname_3d[$valIdx]}]" . $RateConfig_3d->getBetContent($val, $newVal);
                    $fieldArr['orate'] = $rateOld;
                    $fieldArr['nrate'] = $rateNew;
                    setRateRecord($fieldArr);
                }
            }
        }
    }
}
if ($okFlag)
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"OK\"}";
}
else
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"error\"}";
}
exit();
?>